﻿using System;
using System.Collections.Generic;
using System.Linq;
using si.caha.Model;
using si.caha.Repository;
using System.Web;
using System.Text;

namespace si.caha.Helper
{
    public class CategoryHierarchy
    {
        private static StringBuilder htmlMenu;

        private static List<Category> GetCategoriesToTheTopLevel(int CategoryId)
        {
            DataContext dataContext = new DataContext();
            
            List<Category> result = new List<Category>();
            var categoryRepository = new CategoryRepository(dataContext);
            List<Category> categories = categoryRepository.GetAll();

            var category = categories.SingleOrDefault(c => c.CategoryId == CategoryId);
            var categoryLevel = category.Level;

            if (category == null)
                return result;

            result.Add(category);
            if (category.Level == 0)
                return result;

            for (int i = 0; i <= categoryLevel; i++)
            {
                category = categories.SingleOrDefault(c => c.CategoryId == category.CategoryParentId);
                if (category != null)
                    result.Add(category);
            }

            result.Reverse();
            return result;
        }

        private static List<Category> GetCategoriesToTheTopLevel(Item item)
        {
            if (item.Category != null)
                return GetCategoriesToTheTopLevel(item.Category.CategoryId);

            return new List<Category>();
        }

        public static HtmlString GetHtml(String rootTag = "nav", String menuId = "treeView")
        {
            DataContext dataContext = new DataContext();
            var categoryRepository = new CategoryRepository(dataContext);
            var categories = categoryRepository.GetAll();

            htmlMenu = new StringBuilder();
            htmlMenu.AppendLine("<" + rootTag + ">");
            htmlMenu.AppendLine("<ul id=\"" + menuId + "\">");
            foreach (var category in categories.Where(c => c.CategoryParentId == 0))
            {
                GetChildren(category, categories);
            }
            htmlMenu.AppendLine("</ul>");
            htmlMenu.AppendLine("</" + rootTag + ">");

            return new HtmlString(htmlMenu.ToString());
        }

        private static void GetChildren(Category parent, List<Category> categories)
        {
            if (parent == null) return;

            var categorieshierarchy = GetCategoriesToTheTopLevel(parent.CategoryId);
            var categoryHref = BuildCategoryHierarchyString(categorieshierarchy);

            htmlMenu.AppendLine("<li class=\"closed\"><a href=\"/Store" + categoryHref + "\">" + parent.Title + "</a>");

            var children = categories.Where(c => c.CategoryParentId == parent.CategoryId).ToList();
            if (children == null) return;

            for (int i = 0; i < children.Count; i++)
            {
                var child = children.ElementAt(i);
                if (i == 0)
                    htmlMenu.AppendLine("<ul>");

                GetChildren(child, categories);

                // close ul when there's no more children left
                if (i == children.Count - 1)
                    htmlMenu.AppendLine("</ul>");
            }

            // close li like it was started outside the loop
            htmlMenu.AppendLine("</li>");
        }

        public static string GetCategorieshierarchyFromItem(Item item)
        {
            if (item != null && item.Category != null)
            {
                var categorieshierarchy = GetCategoriesToTheTopLevel(item);
                return BuildCategoryHierarchyString(categorieshierarchy);
            }
            return "";
        }

        private static string BuildCategoryHierarchyString(List<Category> CategoryHierarchy)
        {
            string categories = "";
            foreach (var category in CategoryHierarchy)
            {
                categories += "/" + category.GetUrlTitle();
            }
            return categories;
        }
    }
}